package cn.edu.hqu.cst.android.ex3;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnBindService, btnUnbindService, btnGetStatus;
    TextView tvServiceStatus;
    MyService.MyBinder serviceBinder;
    boolean isServiceBind = false;
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            serviceBinder = (MyService.MyBinder) service;
            isServiceBind = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isServiceBind = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnBindService = findViewById(R.id.btn_main_activity_bind_service);
        btnUnbindService = findViewById(R.id.btn_main_activity_unbind_service);
        btnGetStatus = findViewById(R.id.btn_main_activity_get_status);
        tvServiceStatus = findViewById(R.id.tv_main_activity_service_status);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetStatus.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, MyService.class);
        int id = v.getId();
        if (id == R.id.btn_main_activity_bind_service) {
            if (!isServiceBind) {
                Log.d("Button Click", "绑定服务器");
                bindService(intent, conn, BIND_AUTO_CREATE);
                isServiceBind = true;
                tvServiceStatus.setText("服务器已连接");
            }
        } else if (id == R.id.btn_main_activity_get_status) {
            if (isServiceBind) {
                Log.d("Button Click", "获取服务器计数");
                try {
                    int count = serviceBinder.getCount();
                    tvServiceStatus.setText("当前服务状态： " + count);
                } catch (NullPointerException e) {
                    // 如果serviceBinder为空，即服务未连接，显示服务未连接消息
                    tvServiceStatus.setText("服务器未连接");
                }
            } else {
                // 如果服务未绑定，显示服务未连接消息
                tvServiceStatus.setText("服务器未连接");
            }
        } else if (id == R.id.btn_main_activity_unbind_service) {
            if (isServiceBind) {
                Log.d("Button Click", "解绑服务器");
                unbindService(conn);
                isServiceBind = false;
                tvServiceStatus.setText("服务器已解绑");
            }
        }
    }
}
